//	Стоимость ОСАГО
var osagoPrice = 0;
var osagoOptions;
var currentOsagoYear = 0;
var osagoXML;
var disableOsagoCalculation = false;
var osagoURI = '';

function calculateOSAGO() {
	if (disableOsagoCalculation) return;
	if (typeof osagoXML == 'undefined') $.ajax({
		url: "osago/OSAGO.xml?" + rand % 1000,
		async: false,
		success: function (response) {
			osagoXML = response;
			calcOsago(osagoXML);
		}
	});
	else calcOsago(osagoXML);
}

function calcOsago(response) {
	// 	log(arguments.callee.toString().replace(/function\s+(\w+)\((.*\n.*)*/,"$1") + ' - start');
	try {
		var bt;
		var territory;
		var kAgeExp;
		var kPower;
		var xml = $(response);

		//	Базовый тариф
		if ($("#corporatePerson").attr("checked")) bt = xml.find("bt>corporate");
		else bt = xml.find("bt>natural");
		bt = parseFloat(bt.text());

		var town = 'spb';
		//	Территория использования
		if ($('input:checked').filter('[name=town]').val() == 'msc') town = 'moskow';
		territory = parseFloat(xml.find(town).text());
		//	Возраст/стаж
		if (!multiDrive) driversListForCalc = driverList.find('.f-driver');
		var ageExpTmp;
		var expTMP = 100;
		if (multiDrive || $("#corporatePerson").attr("checked")) {
			kAgeExp = xml.find("less22Less3");
			kAgeExp = parseFloat(kAgeExp.text());
		} else {
			var kAgeExpMin = 0;
			var t;
			for (var i = 0; i < driversListForCalc.length; i++) {
				t = driversListForCalc.find('.f-driver-c2:first .input-47');
				var drA = t.eq(0).val();
				var drE = t.eq(1).val();
				drA = parseInt(drA);
				drE = parseInt(drE);
				if (drA <= 22 && drE <= 3) ageExpTmp = "less22Less3";
				else if (drA <= 22 && drE > 3) ageExpTmp = "less22more3";
				else if (drA > 22 && drE <= 3) ageExpTmp = "more22Less3";
				else ageExpTmp = "more22More3";
				if (parseFloat(xml.find(ageExpTmp).eq(0).text()) > kAgeExpMin) kAgeExpMin = parseFloat(xml.find(ageExpTmp).eq(0).text());
			}
			kAgeExp = kAgeExpMin;
		}
		if (isNaN(kAgeExp) || kAgeExp <= 0) kAgeExp = 1;

		//	Мощность двигателя
		var pTMP;
		switch (carPower) {
		case "power50":
			pTMP = 'less50';
			break;
		case "power70":
			pTMP = 'less70';
			break;
		case "power100":
			pTMP = 'less100';
			break;
		case "power120":
			pTMP = 'less120';
			break;
		case "power150":
			pTMP = 'less150';
			break;
		case "power150more":
			pTMP = 'more150';
			break;
		}
		kPower = parseFloat(xml.find("power>" + pTMP).text());
		var malus = 1;
		var curK = '3';
		var countCr = '0';
		var oE = [$('#osagoFirstYear'), $('#osagoSecondYear'), $('#osagoThirdYear'), $('#osagoFourthYear'), $('#osagoFifthYear'), $('#osagoSixthYear'), $('#osagoSeventhYear')];
		for (var i = 0; i < currentOsagoYear; i++) {
			countCr = oE[i].find('div').filter('[clicked="clicked"]').eq(0).text();
			curK = xml.find('bonusMalus>class').filter(function () {
				return $(this).find('value').eq(0).text() == curK;
			}).eq(0).find('e' + countCr.replace('+', '')).eq(0).text();
		}
		malus = xml.find('bonusMalus>class').filter(function () {
			return $(this).find('value').eq(0).text() == curK;
		}).eq(0).find('kBM').eq(0).text();
		malus = parseFloat(malus);
		//	alert('bt='+bt+'\nterritory='+territory+'\nkAgeExp='+kAgeExp+'\nkPower='+kPower+'\nmalus='+malus)
		//	alert('');
		osagoPrice = parseInt(bt * territory * kAgeExp * kPower * malus * 1000) / 1000;
		var intPrice = parseInt(osagoPrice);
		var realPrice = Math.round((osagoPrice - intPrice) * 100);
		if (realPrice < 10) realPrice = '0' + realPrice.toString();
		if (isNaN(osagoPrice)) $("#osagoDiv").css('display', 'none');
		else {
			$("#osagoDiv").css('display', '');
			$("#f-osago-price").find('span:first').html("<b>" + addSpaces(intPrice.toString()) + '.<span style="font-size:smaller;">' + realPrice + '</span></b> р.');
		}
		clientURL = generateURL();
		clientURL = encodeURI(clientURL);
		checkCarCookieDublicate();
		setPrevCalcs();
	} catch (eee) {
		alert('in Osago error')
	};
	// 	log(arguments.callee.toString().replace(/function\s+(\w+)\((.*\n.*)*/,"$1")+' - end');
}

function loadOsagoOptions() {
	$('#f-osago-price2').find('.year:first').find('a').mousedown(function () {
		$(this).siblings().attr('class', '');
		$(this).attr('class', 'active');
		var h = $("#crashCountHeaderText");
		if ($(this).text() == '1') h.css('display', 'none');
		else
		h.css('display', '');
		currentOsagoYear = parseInt($(this).text()) - 1;
		setVisibilityOsagoCrashes();
		//		alert(currentOsagoYear);
		$('#os').css('height', 107 + (parseInt(currentOsagoYear) * 24) + 'px');
		calcOsago(osagoXML);
		return false;
	});
	try {
		var oE = [$('#osagoFirstYear'), $('#osagoSecondYear'), $('#osagoThirdYear'), $('#osagoFourthYear'), $('#osagoFifthYear'), $('#osagoSixthYear'), $('#osagoSeventhYear')];
		for (var i = 0; i < 7; i++)
		oE[i].find('[class="countCrashes"]').mousedown(function () {
			updateCountCrashes(this);
		});
	} catch (e) {
		alert('osago e=' + e);
	}

	if (!multiDrive) {
		driversListForCalc = $('.f-driver');
		var expMin = 100;
		for (var i = 0; i < driversListForCalc.length; i++) {
			var drE = parseInt(driversListForCalc.eq(i).find('.input-47').eq(1).val());
			if (drE < expMin) expMin = drE;
		}

		var osagoAge = expMin < 7 ? expMin + 1 : 7;
		$('.year').find('a').filter(function () {
			return parseInt($(this).text()) == osagoAge
		}).eq(0).mousedown();
	} else
	$('.year').find('a').filter(function () {
		return parseInt($(this).text()) == 1
	}).eq(0).mousedown();
};

function updateCountCrashes(obj) {
	$(obj).siblings().attr('clicked', '').css('font-weight', '').css('color', 'black');
	$(obj).attr('clicked', 'clicked').css('font-weight', 'bold').css('color', '#FF7200');
	calcOsago(osagoXML);
}

function setVisibilityOsagoCrashes() {
	try {
		var oE = [$('#osagoFirstYear'), $('#osagoSecondYear'), $('#osagoThirdYear'), $('#osagoFourthYear'), $('#osagoFifthYear'), $('#osagoSixthYear'), $('#osagoSeventhYear')];

		for (var i = 0; i < 7; i++) oE[i].css('display', 'none');
		for (var i = 0; i < currentOsagoYear; i++) oE[i].css('display', '');
	} catch (eee) {
		alert('osago3:' + eee);
	}
}